package com.Gwo.basic.aop_anno;

import com.Gwo.org.domain.Logging;
import com.Gwo.org.service.IDeptService;
import com.Gwo.org.service.ILoggingService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.InetAddress;
import java.util.*;

@Component
@Aspect
@Order(1)
@Slf4j
public class Log {
    private static final Logger LOGGER = LoggerFactory.getLogger(Log.class);
    @Autowired
    private ILoggingService service;
    @Autowired
    private IDeptService deptService;
    //切点
    @Pointcut("execution(* com.Gwo.org.controller.*Controller.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
    }

    @AfterReturning(value = "pointcut()", returning = "ret")
    public void doAfterReturning(Object ret) throws Throwable {
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取当前请求对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        int before = deptService.findAll().size();
        //记录请求信息
        Logging webLog = new Logging();
        Object result = joinPoint.proceed();
        int after = deptService.findAll().size();
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method.isAnnotationPresent(ApiOperation.class)) {
            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
            webLog.setDetail(apiOperation.value());
        }

        //给ip赋值
        String requestURL = request.getRequestURL().toString();
        String urlStr = request.getRequestURL().toString();
        String s = urlStr.split("/")[2];
        String ip = s.split(":")[0];
        if(ip.equals("localhost")){
            ip= InetAddress.getLocalHost().getHostAddress();
        }
        webLog.setIp(ip);
        //给用户名赋值
        webLog.setUsername("Gwo");
        //给请求的方法名赋值
        webLog.setAction(signature.getName());
        //给请求时间赋值
        webLog.setCreateDate(new Date());
        //给方法细节赋值
        String action = signature.getName();
        if(action.equals("delete")){
            webLog.setDetail("删除了某个数据");
        }else if(action.equals("queryByPage")){
            webLog.setDetail("查询了数据");
        }else if(action.equals("getDeptTree")){
            webLog.setDetail("查找了无限极树");
        }else if(action.equals("addOrUpdate")){
            webLog.setDetail(before==after?"修改了数据":"添加了数据");
        }else if(action.equals("findAll")){
            webLog.setDetail("遍历了列表");
        }else if(action.equals("batchDelete")){
            webLog.setDetail("删除了某些数据");
        }

        service.add(webLog);
        return result;

    }



}
